// Fill out your copyright notice in the Description page of Project Settings.


#include "SectorComponent.h"

// Sets default values for this component's properties
USectorComponent::USectorComponent()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...

	auto Cylinder = LoadObject<UStaticMesh>(nullptr, TEXT("/Script/Engine.StaticMesh'/Engine/BasicShapes/Cylinder.Cylinder'"));
	auto Plane = LoadObject<UStaticMesh>(nullptr, TEXT("/Script/Engine.StaticMesh'/Engine/BasicShapes/Plane.Plane'"));
	
	Sector_Cylinder = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Cylinder"));
	Sector_Cylinder->SetStaticMesh(Cylinder);
	Sector_Cylinder->SetMaterial(0, SectorMaterial);
	Sector_Cylinder->SetupAttachment(this);
	
	MinBackFacePlane = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MinPlane"));
	MinBackFacePlane->SetStaticMesh(Plane);
	MinBackFacePlane->SetupAttachment(this);
	
	MaxBackFacePlane = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MaxPlane"));
	MaxBackFacePlane->SetStaticMesh(Plane);
	MaxBackFacePlane->SetupAttachment(this);
}


// Called when the game starts
void USectorComponent::BeginPlay()
{
	Super::BeginPlay();

	// ...
	
}

void USectorComponent::OnRegister()
{
	Super::OnRegister();

	AActor* Owner = this->GetOwner();
	Owner->AddInstanceComponent(Sector_Cylinder);

	if (SectorMaterial)
	{
		auto MID = UMaterialInstanceDynamic::Create(SectorMaterial, this);
		Sector_Cylinder->SetMaterial(0, MID);
		MID->SetVectorParameterValue(TEXT("Color"), Color);
		
	}
	
}


// Called every frame
void USectorComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// ...
}

